/**
 * Created by ZFL on 2017/12/25.
 */
    function appInit(){
    PageInit();
}
var PageInit = function() {
    if($.getStorage('xmOpenid')&& $.isWX()){

        $.updataInfo($.getStorage('xmOpenid'))

    }
    //系统消息
    $('.me-message').click(function () {
        if(com_obj.userId && com_obj.userId>0){
            window.location.href="../message/message.html";
        }else{
            $.showLogin();
        }
    });
    //查看全部订单
    //我的订单列表
    $('.my-centre-indent-rows a').click(function () {
        var $index = $(this).index();
        if(com_obj.userId && com_obj.userId>0){
            switch ($index){
                case 0:
                    //代付款
                    location.href = '../order/order.html?pay=pay';
                    break;
                case 1:
                    //代发货
                    location.href = '../order/order.html?deliver=deliver';
                    break;
                case 2:
                    //代收货
                    location.href = '../order/order.html?receive=receive';
                    break;
                case 3:
                    //已完成
                    location.href = '../order/order.html?finish=finish';
                    break;
                case 4:
                    //代售后
                    location.href = '/b2c/customerSer/customerSer.html';
                    break;
            }
        }else{
            //登录
            $.showLogin();
        }

    });

    if (com_obj.userId && com_obj.userId>0) {
        //查看订单
        personal();//个人用户信息
        totalNum();//我喜欢的品牌数、邀请小伙伴数、现金币
        selectOrder();//我的订单
    }
    $(".me-but .my-centre-cells").click(function () {
        var index=$(this).index()+1;
        if(com_obj.userId && com_obj.userId>0){
            switch(index){
                case 1:
                    //我的钱包
                    location.href = '../purse/myPurse.html';
                    break;
                case 2:
                    //我的优惠券
                    location.href = '../purse/myCoupons.html';
                    break;
                case 3:
                    //我的个人信息
                    location.href='personal.html';
                    break;
            }
        }else {
            $.showLogin();
        }
    });
};

//加载个人中心信息
function personal() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.userId = com_obj.userId;
            return obj;
        },
        visible: true,
        visibleTime: 0,
        approve: true,
        api: "yg_baijiaapp/user/getUserEntityByIdNoPassword",
        success: function (result) {
            //console.log(result);
            if (result.code == 10000) {
                var data = result.data;
                $(".name").text(data.nickName);
                if (data.picture.toString() == '') {
                    $(".user-pic").attr("src", '../src/images/logo_80.png');
                } else {
                    if (data.picture.toString().indexOf("http") <= -1) {
                        $(".user-pic").attr("src", $.imgApi() + '' + data.picture);
                    } else {
                        $(".user-pic").attr("src", data.picture);
                    }
                }
                if (data.companyId>= 0) {
                    $(".empirical-1").css('display','none');
                    $(".empirical-2").css('display','block');
                    $(".empirical-2 .experience span").text(data.empiricValue);//经验值
                }else{
                    $(".empirical-1 span").text(data.empiricValue);//经验值
                }


                //显示等级
                if(data.level>=1&&data.level<60){
                   $('.me-name .tips').text('VIP');
                    $('.tips').css('display', 'inline-block');   //显示等级
                }else if(data.level>=200&&data.level<600){
                    $('.me-name .tips').text('店主');
                    $('.tips').css('display', 'inline-block');   //显示等级
                }else if(data.level>=600&&data.level7600){
                    $('.me-name .tips').text('共创');
                    $('.tips').css('display', 'inline-block');   //显示等级
                }else if(data.level>=700){
                    $('.me-name .tips').text('慧长');
                    $('.tips').css('display', 'inline-block');   //显示等级
                }else{

                }

            } else {
                $.toast("异常", "text");
            }
        }
    });
}

//我喜欢的品牌数、邀请小伙伴数、现金币
function totalNum() {
    $.ajaxHandler({
        model: function () {
            var obj = {};
            obj.userId = com_obj.userId;
            return obj;


        },
        visible: true,
        visibleTime: 0,
        approve: true,
        api: "yg_baijiaapp/brand/enjoybrandsNumAndInvitationNum",
        success: function (result) {
            if (result.code == 10000) {
                var data = result.data;
                $(".my-purse .fr").text("当前现金币"+data.money+"");//当前现金币
                $(".my-coupon .fr").text('共' + data.couponNum + '张');//优惠券张数
                $(".love-brand .fr").text('共' + data.enjoyBrandNum + '个');//喜欢的品牌数量
            } else {
                $.toast("异常", "text");
            }
        }
    });
}
//我的订单
function selectOrder() {
    $.ajaxHandler({
            model: function () {
                var obj = {};
                obj.userId = com_obj.userId;
                return obj;
            },
            visibleTime: 0,
            dataType: "json",
            approve: true,
            api: 'yg_baijiaapporder/order/getOrderNum',
            success: function (result) {
                //console.log(result);
                if (result.code == 10000) {
                    var data = result.data;
                    // 待付款
                    if (data.needToPayOrderNum > 0) {
                        $('.my-centre-gird-icon-1').append('<span class="num">' + data.needToPayOrderNum  + '</span>');
                    }
                    // 待发货
                    if (data.needToSendGoodsOrderNum > 0) {
                        $('.my-centre-gird-icon-2').append('<span class="num">' + data.needToSendGoodsOrderNum + '</span>');
                    }
                    // 待收货
                    if (data.needToReceiveGoodsOrderNum > 0) {
                        $('.my-centre-gird-icon-3').append('<span class="num">' + data.needToReceiveGoodsOrderNum + '</span>');
                    }
                    // 待评价
                    if(data.needToJudgeOfOrderNum > 0){
                        $('.my-centre-gird-icon-4').append('<span class="num">' + data.needToJudgeOfOrderNum + '</span>');
                    }
                    // 售后
                    if (data.afterSalesOrderNum > 0) {
                        $('.my-centre-gird-icon-5').append('<span class="num">' + data.afterSalesOrderNum + '</span>');
                    }
                } else {
                    $.toast('订单异常');
                }

            }
        }
    );
}

//查看全部订单
function orderList(){
    if(com_obj.userId && com_obj.userId>0){
        window.location.href = '../order/order.html';
    }else{
        $.showLogin();
    }
}

//查看基本个人信息
function personFun(){
    if(com_obj.userId && com_obj.userId>0){
        window.location.href = 'personal.html';
    }else {
        $.showLogin();
    }
}

//进入系统设置
function systemFun(){
    if(com_obj.userId && com_obj.userId>0){
        window.location.href = '../system/index.html';
    }else {
        $.showLogin();
    }
}



//点击悬浮的导航栏
$('.fixed-tab .rightMore span').click(function(e){
    e.preventDefault()
    $('.fixed-tab').fadeOut();
    $('.external').css('top','1rem');
    $('.me-imgs-box').css('margin-top','1.4rem');
    $('.me-head').css('height','5rem')
})



